package View.JTable;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;
import javax.swing.table.DefaultTableCellRenderer;

public class MyTableCellRenderer extends DefaultTableCellRenderer {
	private ImageIcon ii1 = new ImageIcon("./property/right.png");
	private ImageIcon ii2 = new ImageIcon("./property/wrong.png");
	private FileSystemView fsv = FileSystemView.getFileSystemView();
	private boolean isSelected = false;
	private boolean isFocus = false;
    private static final Color COLOR1 = new Color(153, 222, 253); //color for border
    private static final Color COLOR2 = new Color(213, 239, 252); //color for up
    private static final Color COLOR3 = new Color(241, 248, 253); //color for down
    private static final Color COLOR4 = new Color(57, 155, 236);  //color for word when selected
    private static final Color COLOR5 = new Color(91, 118, 173);  //color for word when not selected
    protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
    private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
    private int style = 0;
    
	public MyTableCellRenderer() {
		super();
		setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
	}

	/**
	 * set the value;
	 */
	@Override
    public void setValue(Object value) {
        if ((value != null) && (value instanceof Boolean)) {
        	style = 1;
        	setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        	if ((Boolean)value == true) {
        		setIcon(ii1);
        	}
        	else if ((Boolean)value == false) {
        		setIcon(ii2);
        	}
        } 
        else if ((value != null) && (value instanceof File)) {
        	Icon icon = fsv.getSystemIcon((File) value);
        	setIcon(icon);
	        setText(((File) value).getName());
        }
        else if ((value != null) && (value instanceof Long)) {
        	style = 2;
        	setText(value.toString() + "M");
        }
        else {
            super.setValue(value);
        }
    } 

	/**
	 * 为提高性能重写
	 */
	@Override
    public boolean isOpaque() { 
    	return false; 
    }
	
	/**
	 * getTableCellRendererComponent;
	 */
	@Override
    public Component getTableCellRendererComponent(JTable table, 
										    		Object value,
										            boolean isSelected, 
										            boolean hasFocus, 
										            int row, 
										            int column) {
		this.isSelected = isSelected;
		this.isFocus = hasFocus;
		
        Color fg = null;
        Color bg = null;

        JTable.DropLocation dropLocation = table.getDropLocation();
        if (dropLocation != null
                && !dropLocation.isInsertRow()
                && !dropLocation.isInsertColumn()
                && dropLocation.getRow() == row
                && dropLocation.getColumn() == column) {

            fg = UIManager.getColor("Table.dropCellForeground");
            bg = UIManager.getColor("Table.dropCellBackground");

            isSelected = true;
        }
        
        setForeground(COLOR5);
		setFont(table.getFont());

        setValue(value); 
        return this;
	}
	
	/**
	 * 为提高外观重写
	 */
	@Override
	public void paint(Graphics g) {
		if (isSelected) {
			setForeground(COLOR4);
		    int W = getWidth();
		    int H = getHeight();
	        
		    GradientPaint GP = new GradientPaint(0, 0, COLOR3, 0, getHeight(), COLOR2, true);
		    Graphics2D g2d 	= (Graphics2D) g.create();

		    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
		    g2d.setPaint(GP);
		    
			if (style == 0) {
			    g2d.fillRect(0, 0, W, H);
			    g2d.setColor(COLOR1);
			   	g2d.drawRect(0, 0, W, H);
			}
			else if (style == 1) {
	            RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, W - 1 + 20, H - 1, 20, 20);
	            g2d.clip(r2d);
	            g2d.fillRect(0, 0, W, H);
	            g2d.setColor(COLOR1);
	            g2d.drawRect(0, 0, W, H);
			}
			if (style == 2) {
				RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(-20, 0, W - 1 + 20, H - 1, 20, 20);
	            g2d.clip(r2d);
	            g2d.fillRect(0, 0, W, H);
	            g2d.setColor(COLOR1);
	            g2d.drawRect(0, 0, W, H);
			}
            g2d.dispose();
		}
		if (isFocus) {
			setFont(getFont().deriveFont(Font.BOLD));
		}
		super.paint(g);
	}

	/**
	 * getNoFocusBorder;
	 */
    private static Border getNoFocusBorder() {
        if (System.getSecurityManager() != null) {
            return SAFE_NO_FOCUS_BORDER;
        } else {
            return noFocusBorder;
        }
    }
}
